const router = require('koa-router')()

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/corsApi', async (ctx, next) => {
  ctx.body = 'cors - wrong'
})
router.get('/proxyApi', async (ctx, next) => {
  ctx.body = 'proxy - OK !!!!!'
})

router.get('/backendApi', async (ctx, next) => {
  ctx.set("Access-Control-Allow-Origin", '*')
  ctx.body = 'backend - OK !!!!!'
})

router.post('/preflight', async (ctx, next) => {
  ctx.set("Access-Control-Allow-Origin", '*')
  ctx.set("Access-Control-Allow-Credentials", true);
  ctx.set("Access-Control-Max-Age", 86400000);
  ctx.set("Access-Control-Allow-Methods", "OPTIONS, HEAD, POST");
  ctx.set("Access-Control-Allow-Headers", "Authorization");

  ctx.body = {
    title: 'preflight'
  }
})

module.exports = router
